package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserSericerImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Override
    public User Login(String phone, String userCode, String sessionCode) {
        //1.传递过来的手机号,用户输入验证码与session的验证码
        //2.小燕验证码是否正确,如果不正确返回验证码错误
        User loginUser = userMapper.findByPhone(phone);
        if (loginUser != null) {
            if (loginUser.getStatus() == 1) {
                throw new BusinessException("用户被禁用");
            }
        }else {
            //如果找不到,完善用户数据
            loginUser = new User();
            loginUser.setStatus(0);
            loginUser.setPhone(phone);
            userMapper.save(loginUser);
        }
        return loginUser;
    }
}
